using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace gdv
{
    public class GessnerStephanSolarSystem : Microsoft.Xna.Framework.DrawableGameComponent
    {
        GraphicsDeviceManager graphics;
        Model moon;
        Model earth;
        Model sun;
        Matrix worldMatrix;
        Matrix viewMatrix;
        Matrix projectionMatrix;
        float rotation = 0.0f;
        float rspeed = 0.05f;
        int direction = 1;
        bool enterReleased = true;
        float viewAngle = 5.0f;
        float turnAngle = 0.0f;

        public GessnerStephanSolarSystem(Game game) : base(game)
        {
          //  graphics = new GraphicsDeviceManager(game); // Changed by PG
          //  Game.Content.RootDirectory = "Content";   // Changed by PG
        }

        public override void Initialize()
        {
            base.Initialize();
            worldMatrix = Matrix.CreateRotationY(4.0f) * Matrix.CreateTranslation(new Vector3(1.0f, 1.0f, 1.0f));
            //viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up);
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)Game.Window.ClientBounds.Width / (float)Game.Window.ClientBounds.Height, 1, 1000);
        }

        protected override void LoadContent()
        {
            sun = Game.Content.Load<Model>("sphere");
            earth = Game.Content.Load<Model>("sphere");
            moon = Game.Content.Load<Model>("sphere");
        }

        public override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape)) Game.Exit();

            //zoom on up / down
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up)) viewAngle -= 0.05f;
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down)) viewAngle += 0.05f;

            //view from side or from top on Left / right
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right)) turnAngle -= 0.05f;
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left)) turnAngle += 0.05f;

            //rotation speed on + / -
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.OemPlus)) rspeed += 0.001f;
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.OemMinus)) rspeed -= 0.001f;

            //no repeated turns of direction when pressing space
            if (enterReleased) if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Space)) { direction *= -1; enterReleased = false; }
            if (Keyboard.GetState(PlayerIndex.One).IsKeyUp(Keys.Space)) enterReleased = true;

            rotation += rspeed * direction;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.CornflowerBlue);
            viewMatrix = Matrix.CreateLookAt(new Vector3(0, turnAngle, viewAngle), Vector3.Zero, Vector3.Up);

            foreach (ModelMesh mesh in sun.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = Matrix.CreateScale(0.4f) * Matrix.CreateRotationY(rotation);
                    effect.View = viewMatrix;
                    //effect.Texture = sunTexture;
                    //effect.TextureEnabled = true;

                    effect.Projection = projectionMatrix;
                }
                mesh.Draw();
            }

            foreach (ModelMesh mesh in earth.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = Matrix.CreateScale(0.4f) * Matrix.CreateRotationY(rotation) * Matrix.CreateScale(0.4f) * Matrix.CreateTranslation(1.2f, 0, 0) * Matrix.CreateRotationY(rotation);
                    effect.View = viewMatrix;

                    effect.Projection = projectionMatrix;
                }
                mesh.Draw();
            }

            foreach (ModelMesh mesh in moon.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = Matrix.CreateScale(0.4f) * Matrix.CreateRotationY(rotation) * Matrix.CreateScale(0.4f) * Matrix.CreateTranslation(1.2f, 0, 0) * Matrix.CreateRotationY(rotation) * Matrix.CreateScale(0.4f) * Matrix.CreateTranslation(1.2f, 0, 0) * Matrix.CreateRotationY(rotation);
                    effect.View = viewMatrix;

                    effect.Projection = projectionMatrix;
                }
                mesh.Draw();
            }
        }
    }
}
